package org.mattpayne.jrac.people.daoimpl;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.mattpayne.jrac.people.dao.PeopleDao;
import org.mattpayne.jrac.people.dao.PeopleDaoExeception;

import bus.Person;

public class TabDelimitedDao implements PeopleDao {

	private String fileName;

	// / Add a new person....
	public void addPerson(Person person) throws PeopleDaoExeception {
		List lst = getPeople();
		lst.add(person);
		try {
			saveAll(lst);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new PeopleDaoExeception("Problem saving: "+ e);
		}

	}

	public void deletePerson(Person person) throws PeopleDaoExeception {

		List lst = getPeople();
		lst.remove(person);
		try {
			saveAll(lst);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new PeopleDaoExeception("Could not delete " + person
					+ " from file " + fileName + " problem exception=" + e);
		}
	}

	public List getPeople() {
		List lstPeople = new ArrayList();
		try {
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			String str;

			while ((str = in.readLine()) != null) {
				lstPeople.add(parse(str));
			}
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lstPeople;
	}

	// / Parse a line of tab delimented text into a person....
	private Person parse(String str) {
		StringTokenizer parser = new StringTokenizer(str, "\t");
		String name = parser.nextToken();
		String email = parser.nextToken();
		String role = parser.nextToken();
		Person p = new Person();
		p.setName(name);
		p.setEmail(email);
		p.setRole(role);
		return p;
	}
	
	private void saveAll(List lst) throws IOException {
		PrintWriter out = new PrintWriter(new FileWriter(fileName));
		for (Iterator it = lst.iterator(); it.hasNext(); ) {
			Person p = (Person) it.next();
			save(out,p);
		}
		out.close();
	
	}
	

	private void save(PrintWriter out, Person p) {
		out.println(p.getName()+"\t"+p.getEmail()+"\t"+p.getRole());
	}

	public void savePerson(Person person) throws PeopleDaoExeception {
		List lstPeople = getPeople();
		lstPeople = null != lstPeople ? lstPeople : new ArrayList();
		Person oldPerson = null;
		boolean bFound = false;
			// the email is considered the key....
			for (Iterator it = lstPeople.iterator(); it.hasNext();) {
				oldPerson = (Person) it.next();
				String email = oldPerson.getEmail();
				email = "" + email; // TODO: If we keep using the XmlDao find a better way.
				// My guess is we will soon be onto a better MySqlDao!
				if (email.equalsIgnoreCase(person.getEmail())) {
					bFound=true;
					break;
				}
			}
		
		if (bFound) {
			lstPeople.remove(oldPerson);
		}
		lstPeople.add(person);
		try {
			saveAll(lstPeople);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setFileName(String file_name) {
		this.fileName = file_name;
	}

}
